<?php defined("MAIN_DIR") or die("No direct access");

$limit = 10;
$page = UV::rInt(strip_tags($route->GET("page")));
if ($page < 1){
	$page = 1;
}

$query = "select SQL_CALC_FOUND_ROWS `Q`.*,`B`.`name`,COUNT(`R`.`mark`) as `rating`
    from `quotes` as `Q`
    left join `ratings` as `R`
        on `Q`.`id` = `R`.`quote_id`
    left join `books` as `B`
        on `Q`.`book_id` = `B`.`id`
    where
        `Q`.`status` = 'placed'
    group by `Q`.`id`
    limit 100";
$list = $db->getTable($query);

$count = count($list);
$all_count = $db->getCell("SELECT FOUND_ROWS()");

require_once ADMIN.HEADFILE;
?>

<h1>Цитаты <a href='/admin/quotes/add/' class='button'>Добавить цитату</a></h1>

<?php
if ($count){
	echo "<table class='statistics'>";
	echo "<tr><td class='c'>ID</td><td>Цитата</td><td>Источник</td><td class='c'>Рейтинг</td><td class='c'>Просмотров</td><td width='110px'>Действия</td></tr>";
	foreach ($list as $quote){
		echo "<tr>";
        echo "<td class='c'>".$quote['id']."</td>";
		echo "<td class='text'>";
			echo "<p>".cropText(strip_tags($quote['text']),100)."</p>";
		echo "</td>";
		echo "<td>".$quote['name']."</td>";
		echo "<td class='c'>".$quote['rating']."</td>";
		echo "<td class='c'>".$quote['looks']."</td>";
		echo "<td>";
			echo "<a target='_blank' href='/{$quote['id']}'><img src='/files/img/admin/preview.png' alt='Посмотреть' title='Посмотреть'></a> ";
			echo "<a class='action-edit' href='/admin/quotes/edit/{$quote['id']}'><img src='/files/img/admin/edit.png' alt='Редактировать' title='Редактировать'></a> ";
			echo "<a class='action-draft' href='/admin/quotes/actions?action=draft&id[]={$quote['id']}'><img src='/files/img/admin/draft.png' alt='В черновики' title='В черновики'></a> ";
			echo "<a class='action-trash' href='/admin/quotes/actions?action=trash&id[]={$quote['id']}'><img src='/files/img/admin/trash.png' alt='В корзину' title='В корзину'></a>";
		echo "</td>";
		echo "</tr>";
	}
	echo "</table>";
}
echo showPages($all_count, $limit);

require_once ADMIN.FOOTERFILE; ?>